﻿using System;
using System.Collections;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Xml;

public class AlipayFunction
{
    public static string[] Arg_sort(string[] sArray)
    {
        for (int i = 0; i < sArray.Length; i++)
        {
            bool flag = false;
            for (int j = sArray.Length - 2; j >= i; j--)
            {
                if (string.CompareOrdinal(sArray[j + 1], sArray[j]) < 0)
                {
                    string str = sArray[j + 1];
                    sArray[j + 1] = sArray[j];
                    sArray[j] = str;
                    flag = true;
                }
            }
            if (!flag)
            {
                return sArray;
            }
        }
        return sArray;
    }

    public static string Build_mysign(ArrayList sArray, string key, string sign_type, string _input_charset)
    {
        string str = Create_linkstring(sArray);
        int length = str.Length;
        return Sign(str.Substring(0, length - 1) + key, sign_type, _input_charset);
    }

    public static string Create_linkstring(ArrayList sArray)
    {
        int count = sArray.Count;
        int num2 = 0;
        StringBuilder builder = new StringBuilder();
        for (num2 = 0; num2 < count; num2++)
        {
            builder.Append(sArray[num2].ToString() + "&");
        }
        return builder.ToString();
    }

    public static string Create_linkstring_utf8(ArrayList sArray)
    {
        int count = sArray.Count;
        int num2 = 0;
        StringBuilder builder = new StringBuilder();
        for (num2 = 0; num2 < count; num2++)
        {
            int index = sArray[num2].ToString().IndexOf('=');
            int length = sArray[num2].ToString().Length;
            string str = sArray[num2].ToString().Substring(0, index);
            string str2 = "";
            if ((index + 1) < length)
            {
                str2 = sArray[num2].ToString().Substring(index + 1);
            }
            switch (str)
            {
                case "subject":
                case "body":
                case "extra_common_param":
                case "royalty_parameters":
                    builder.Append(str + "=" + HttpUtility.UrlEncode(str2) + "&");
                    break;

                default:
                    builder.Append(sArray[num2].ToString() + "&");
                    break;
            }
        }
        return builder.ToString();
    }

    public static void log_result(string sPath, string sWord)
    {
        StreamWriter writer = new StreamWriter(sPath, false, Encoding.Default);
        writer.Write(sWord);
        writer.Close();
    }

    public static ArrayList Para_filter(ArrayList sArray)
    {
        int count = sArray.Count;
        for (int i = 0; i < count; i++)
        {
            int index = sArray[i].ToString().IndexOf('=');
            int length = sArray[i].ToString().Length;
            string str = sArray[i].ToString().Substring(0, index);
            string str2 = "";
            if ((index + 1) < length)
            {
                str2 = sArray[i].ToString().Substring(index + 1);
            }
            if (((str.ToLower() == "sign") || (str.ToLower() == "sign_type")) || (str2 == ""))
            {
                sArray.RemoveAt(i);
                count--;
                i--;
            }
        }
        return sArray;
    }

    public static string Query_timestamp(string partner)
    {
        string url = "https://mapi.alipay.com/gateway.do?service=query_timestamp&partner=" + partner;
        XmlTextReader reader = new XmlTextReader(url);
        XmlDocument document = new XmlDocument();
        document.Load(reader);
        return document.SelectSingleNode("/alipay/response/timestamp/encrypt_key").InnerText;
    }

    public static string Sign(string prestr, string sign_type, string _input_charset)
    {
        StringBuilder builder = new StringBuilder(0x20);
        if (sign_type.ToUpper() == "MD5")
        {
            byte[] buffer = new MD5CryptoServiceProvider().ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(prestr));
            for (int i = 0; i < buffer.Length; i++)
            {
                builder.Append(buffer[i].ToString("x").PadLeft(2, '0'));
            }
        }
        return builder.ToString();
    }
}

